package markerSession;

import java.rmi.RemoteException;
import java.util.Collection;

import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import marker.Marker;
import marker.MarkerHome;
import student.Student;
import student.StudentHome;
import user.User;
import user.UserHome;
import descriptor.MarkerDescriptor;
import descriptor.StudentListDescriptor;

public class MarkerSessionBean implements SessionBean{
	InitialContext initialContext;
	UserHome userHome;
	StudentHome studentHome;
	MarkerHome markerHome;
	Marker Marker;
	int markerId;

	// <ATTRIBUTES>
	
	// simple attributes
	public int getId() throws RemoteException{
		return markerId;
		//return Marker.getId();
	}
	public void setId(int id) throws RemoteException{
		Marker.setId(id);
	}
	
	public String getLogin() throws RemoteException{
		return Marker.getLogin();
	}
	public void setLogin(String login) throws RemoteException{
		Marker.setLogin(login);
	}
	
	public String getPassword() throws RemoteException{
		return Marker.getPassword();
	}
	public void setPassword(String password) throws RemoteException{
		Marker.setPassword(password);
	}
	
	public String getFirstname() throws RemoteException{
		return Marker.getFirstname();
	}
	public void setFirstname(String firstname) throws RemoteException{
		Marker.setFirstname(firstname);
	}
	
	public String getSurname() throws RemoteException{
		return Marker.getSurname();
	}
	public void setSurname(String surname) throws RemoteException{
		Marker.setSurname(surname);
	}
	
	public String getEmail() throws RemoteException{
		return Marker.getEmail();
	}
	public void setEmail(String email) throws RemoteException{
		Marker.setEmail(email);
	}
	
	// Bean attributes
	// relations 1-1
	public User getUser() throws RemoteException{
		return Marker.getUser();
	}
	public void setUser(User user) throws RemoteException{
		Marker.setUser(user);
	}
	
	
	public Collection getStudents(){

		try {
			System.out.println("home : "+studentHome.findAllStudents().size()+"\n\n\n");
			return studentHome.findAllStudents();
		} catch (FinderException e) {
			
			e.printStackTrace();
		}
		return null;
	}
	public StudentListDescriptor getStudentListDescriptor() {
		StudentListDescriptor list = new StudentListDescriptor();
		for(Object student : getStudents()){
			list.addStudent(((Student)student).getDescriptor());
		}
		return list;
	}
	
	public MarkerDescriptor getDescriptor(){
		MarkerDescriptor desc = null;
		try {
			desc= new MarkerDescriptor(getId(),getFirstname(),getSurname(),getLogin(),getPassword(),getEmail());
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return desc;
	}
	public void ejbActivate() throws EJBException, RemoteException {
		
		
	}

	public void ejbPassivate() throws EJBException, RemoteException {
		
		
	}

	public void ejbRemove() throws EJBException, RemoteException {
		
		
	}

	public void setSessionContext(SessionContext arg0) throws EJBException, RemoteException {
		
		
	}
	public String ejbCreate(int id){
		try {
			initialContext = new InitialContext();
			markerHome = (MarkerHome)initialContext.lookup("MyMarker_L");
			studentHome = (StudentHome)initialContext.lookup("MyStudent_L");
			Marker = markerHome.findByPrimaryKey(id);
			markerId = id;
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return null;
	}
	public void ejbPostCreate(int id){

	}

}
